#!/usr/bin/env python
#-*- coding: utf-8 -*-

import urllib2
import urllib
import re
import os

from time import sleep

#weatherURL="http://qq.ip138.com/weather/guangdong/DianBai.htm"
DianbaiWeatherURL="http://jj.stu.edu.cn/jj2009/myown/dl.asp?url=http://www.nmc.gov.cn/publish/forecast/AGD/dianbai.html"
ShantouWeatherURL="http://jj.stu.edu.cn/jj2009/myown/dl.asp?url=http://www.nmc.gov.cn/publish/forecast/AGD/shantou.html"


def removeHtmlTag(htmlContent):
    reHtmlTag = re.compile('</?\w+[^>]*>') #< /? \w+ [^>] * >
    text = reHtmlTag.sub('',htmlContent) #去掉HTML 标签
    return text

def removeWind(windContent):
    reWind1 = re.compile('<div style="line-height:17px">.+[^>]<br/>.[^>]+</div>')
    reWind2 = re.compile('<div class="windDayLeft">.+<br/>.[^>]+</div>')
    reWind3 = re.compile('<div class="windDay">.+<br/>.[^>]+</div>')
    noWind1 = reWind1.sub('',windContent)
    noWind2 = reWind2.sub('',noWind1)
    noWind = reWind3.sub('',noWind2)
    return noWind

def removeHtmlSpace(spaceContent):
    reSpace = re.compile('\s[^]')
    noSpace = reSpace.sub('', spaceContent)
    return noSpace

def getWeather(weatherURL):

    req=urllib2.Request(url=weatherURL)
    conn=urllib2.urlopen(req)
    header=str(conn.info())
    res=conn.read().decode('gb2312').encode('utf-8')
    all=res.split('<div class="everyDayelse">')[1].split('<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>')[0]
    #all=res.split('<div class="weatherlistContent">')[1].split('<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>')[0]
    noWind=removeWind(all)
    #print noWind
    allList=removeHtmlTag(noWind).strip().replace(' ','').replace('高温','').replace('低温','~').replace('白天','').replace('夜晚','').replace('星期', '周').replace('周天','周日')
    #print allList
    t=re.findall('\S+',allList)
    #for i in t:
        #print i
    
    today=t[0]
    toDayWeather=t[1]
    toNightWeather=t[2]
    toDayTempl=t[3]
    toNightTempl=t[4]
    if toDayWeather == toNightWeather:
        toDateWeather=toDayWeather
    else:
        toDateWeather=toDayWeather+'~'+toNightWeather
        
    secondDate=t[3]
    secondDayWeather=t[4]
    secondNightWeather=t[5]
    secondDateTempl=t[6]
    if secondDayWeather == secondNightWeather:
        secondDateWeather=secondDayWeather
    else:
        secondDateWeather=secondDayWeather+'~'+secondNightWeather
        
    thirdDate=t[7]
    thirdDayWeather=t[8]
    thirdNightWeather=t[9]
    thirdDateTempl=t[10]
    if thirdDayWeather == thirdNightWeather:
        thirdDateWeather=thirdDayWeather
    else:
        thirdDateWeather=thirdDayWeather+'~'+thirdNightWeather
   
    fourthDate=t[11]
    fourthDayWeather=t[12]
    fourthNightWeather=t[13]
    fourthDateTempl=t[14]
    if fourthDayWeather == fourthNightWeather:
        fourthDateWeather=fourthDayWeather
    else:
        fourthDateWeather=fourthDayWeather+'~'+fourthNightWeather
    
    #head='未来两天天气:'
    head=''
    #todayMsg=today+':'+toDateWeather+','+toDayTempl+toNightTempl
    secondMsg=secondDate+':'+secondDateWeather+','+secondDateTempl
    thirdMsg=thirdDate+':'+thirdDateWeather+','+thirdDateTempl
    fourthMsg=fourthDate+':'+fourthDateWeather+','+fourthDateTempl

    #fourthMsg=fourthDate+':'+fourthDateTempl+','+fourthDay+':'+fourthDayWeather+','+fourthNight+':'+fourthNightWeather
    #msg=head+todayMsg+os.linesep+secondMsg+os.linesep+thirdMsg+os.linesep+fourthMsg+'.'
    msg=head+secondMsg+';'+thirdMsg+';'+fourthMsg+'.'
    #print len(msg)
    
    #print msg
    
    return msg
    #noSpace=removeHtmlSpace(allList)
    #print noSpace
    #exit(1)
    #fobj=open('./weather.txt', 'r')
    #contentList=fobj.readlines()
    #contentStr=''.join(contentList)
#print contentStr
def sendWeather(user, passwd, sendto, msg, url):
    message=msg.decode('utf-8').encode('gb2312')
    body=[('username','%s'% user),('password','%s'% passwd),('to','%s'% sendto),('message','%s'% message)]
    data = urllib.urlencode(body)
    req=urllib2.Request(url=url, data=data)
    conn=urllib2.urlopen(req)
    #header=str(conn.info())
    #res=conn.read()
    #print header
    #print res

if __name__=="__main__":

    user='151*******0'
    passwd='love******'
    sendtoMe='159*******'
    sendtoCL='151********'
    url='http://blbear.com/api/fetion/fetion.php'
    Dianbai = getWeather(DianbaiWeatherURL)
    isGet=False
    while not isGet:
        try:
            Dianbai = getWeather(DianbaiWeatherURL)
            Shantou = getWeather(ShantouWeatherURL)
            DianbaiMsg = "电白:"+Dianbai
            ShantouMsg = "汕头:"+Shantou
            isGet=True
        except:
            isGet=False
    #print DianbaiMsg
    #print ShantouMsg
    #print len(DianbaiMsg)
    sendWeather(user, passwd, sendtoMe+','+sendtoCL, DianbaiMsg, url)
    #print "send"
    sleep(10)
    sendWeather(user, passwd, sendtoMe, ShantouMsg, url)
    #print "send"
    #sleep(86400)#one day
